package net.stevechaloner.idea.ffs.actions;

import com.intellij.openapi.actionSystem.ActionGroup;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @author Steve Chaloner
 */
public class SearchGroupActionGroup extends ActionGroup
{
    /**
     * 
     */
    private static final Comparator<AnAction> NAME_COMPARATOR = new Comparator<AnAction>()
    {

        public int compare(AnAction o1,
                           AnAction o2)
        {
            String text1 = o1.getTemplatePresentation().getText();
            String text2 = o2.getTemplatePresentation().getText();
            return text1.compareToIgnoreCase(text2);
        }
    };

    /**
     * All available actions.
     */
    private final List<AnAction> actions = new ArrayList<AnAction>();

    // javadoc unnecessary
    public void addChild(@NotNull AnAction child)
    {
        actions.add(child);
        Collections.sort(actions,
                         NAME_COMPARATOR);
    }

    // javadoc unnecessary
    public boolean removeChild(AnAction child)
    {
        return actions.remove(child);
    }

    // javadoc inherited
    public AnAction[] getChildren(@Nullable AnActionEvent anActionEvent)
    {
        return actions.toArray(new AnAction[actions.size()]);
    }

    // javadoc unnecessary
    public void removeAllActions()
    {
        actions.clear();
    }
}
